﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MT.WindowsUI.NavigationPane;
using System.Drawing;
namespace TinyERP.Framework.Controls
{
    public class OutlookNavigator : NavigateBar, INavigator
    {
    
        public OutlookNavigator()
            : base()
        {
            this.IsShowCollapseButton = false;
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ResumeLayout(false);
        }

        #region INavigator Members

        public event SelectNodeHandler OnSelectedNode;

        public void SelectNode(ApplicationNode formNode)
        {
            
        }

        public void AddNode(ApplicationNode formNode)
        {
            NavigateBarButton button = new NavigateBarButton();
            button.Caption = formNode.Title;
            button.IsShowCaptionDescription = false;
            button.Tag = formNode;

            if (formNode.Children.Count == 0)
            {
                button.IsActionButtonOnly = true;
            }
            else
            {
                OutlookBar outlookBar = new OutlookBar();
                button.RelatedControl = outlookBar;
                foreach (ApplicationNode childNode in formNode.Children)
                {
                    OutlookBarItem outlookBarItem = new OutlookBarItem();
                    outlookBarItem.Values.ExtraText = childNode.Title;
                    outlookBarItem.Values.Text = "";
                    outlookBarItem.Tag = childNode;
                    outlookBarItem.Click += new EventHandler(OnOutlookBarItemClick);
                    System.Drawing.Image icon = IconManager.GetInstance().GetAppIcon(childNode.IconKey);
                    if (icon == null)
                    {
                        icon = IconManager.GetInstance().GetAppIcon("default");
                    }
                    outlookBarItem.Values.Image = icon;
                    outlookBar.Controls.Add(outlookBarItem);
                }
            }
            if (!string.IsNullOrEmpty(formNode.UIClassName))
            {
                button.OnNavigateBarButtonSelected += new NavigateBarButton.OnNavigateBarButtonSelectedEventHandler(OnButtonSelected);
            }
            this.NavigateBarButtons.Add(button);
        }

        private void OnOutlookBarItemClick(object sender, EventArgs e)
        {
            ApplicationNode formNode = ((OutlookBarItem)sender).Tag as ApplicationNode;
            if (OnSelectedNode != null)
            {
                OnSelectedNode(sender, formNode);
            }
        }

        private void OnButtonSelected(NavigateBarButtonEventArgs e)
        {
            ApplicationNode formNode = (ApplicationNode)e.NavigateBarButton.Tag;
            if (OnSelectedNode != null)
            {
                OnSelectedNode(e.NavigateBarButton, formNode);
            }
        }
        
        #endregion
    }
}
